var express = require('express');
var router = express.Router();
//引入上传文件的方法
const {uploadFile} = require("../src/utils/upload");
//引入业务层
const studentService = require("../src/services/student/studentService")
//添加学生
router.post("/addstudent",async (req,resp,next)=>{
    //上传文件
    let res = await uploadFile(req,{fileType:"images"});
    console.log(res);
    //构建添加学生的数据
    //1 获得上传文件的名字  '/photos/c94eeece189e6.jpg'
    let photo = res.data.url.split("/")[2];
    console.log("返回的值===",photo)
    //添加学生
    let obj ={}; //学生对象
    //解构参数的值
    let {name,age} = res.data;
    //构建学生对象
    obj={name,age,photo};
    //studentService.addStudent({"name":res.data.name,"age":res.data.age,"photo":photo})
     let add_res = studentService.addStudent(obj);
     if(add_res!=null){
        resp.json({"code":0,"msg":"添加成功"});
     }else{
        resp.json({"code":1,"msg":"添加失败"});
     }  
})

module.exports = router;
